<?php
/**
 *---------------------------------------------------------------
 * 檔案名稱: blog.php
 * 類別功能: blog。 
 *---------------------------------------------------------------
 */

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Blog extends CI_Controller {
	
	//建構方法
	function __construct() {
		parent::__construct();
		$this->load->database();
		$this->load->helper(array('form','url','date'));
		$this->load->library(array('form_validation','session'));
		
		//取得session值
		$login = $this->session->userdata('login');
		
		//依session值,從模型中取得會員資料
		$this->load->model('frontend/blog_model');		
		$this->info = $this->blog_model->get_info($login);	    				 
	}
	
	 //預設方法
	function index(){		
		
		//blog文章分類
		$csql = "SELECT * FROM `ci_category` WHERE `cuser`=? ORDER BY `cdate` DESC LIMIT 5";
	  	$cqry = $this->db->query($csql,array($this->info['login']));
        	  	
		//blog文章列表
		$sql = "SELECT * FROM `ci_article` WHERE `cuser`=? ORDER BY `cdate` DESC LIMIT 5";		
		$qry = $this->db->query($sql,array($this->info['login']));				
		$rlt['data'] = array(
		                    'category' => $cqry->result_array(), 
		                    'article' => $qry->result_array()
	                   );
	     
		$this->load->view('frontend/default/header',$this->info);
		$this->load->view('frontend/blog',$rlt);
		$this->load->view('frontend/default/footer');
	}
	
	//新增方法
	function add(){
				
		if( !count($_POST) > 0 ) { //如果沒有post動作 ,就載入預設blog新增頁面
			
		   $this->load->view('frontend/default/header',$this->info);
		   $this->load->view('frontend/blog_add');
		   $this->load->view('frontend/default/footer');
		}else{  //否則 ,處理post程序
			
			//表單規則驗證
			$this->form_validation->set_rules('category_id','文章分類','trim|required');		
			$this->form_validation->set_rules('cdate','建立日期','trim|required');
			$this->form_validation->set_rules('title','標題','trim|required');
			$this->form_validation->set_rules('content','內容','trim|required');
			$this->form_validation->set_rules('cuser','使用者','trim|required');
			
			if($this->form_validation->run() === FALSE){
				
				$this->load->view('frontend/default/header',$this->info);
		        $this->load->view('frontend/blog_add');
		        $this->load->view('frontend/default/footer');
			}else{
                
				//post資料陣列
				$data = array(
				    'category_id' => $this->input->post('category_id'),
				          'cdate' => $this->input->post('cdate'),
				          'title' => $this->input->post('title'),
				        'content' => $this->input->post('content'),
				          'cuser' => $this->input->post('cuser')
				);
				
				//新增到資料庫
				$ins_rlt = $this->db->insert('ci_article',$data) ;
				
				if($ins_rlt){ //判斷是新增成功					
		            echo "<script>javascript:alert('恭喜!文章新增成功!'); window.location = '".site_url('frontend/blog')."'</script>";
				}else{
					echo "<script>javascript:alert('抱歉！文章新增失敗！');</script>";
				}				
		
			}
		}		 	
	}
	
	//編輯方法
	function edit($id){		
		
		//接收GET值(文章id)
		//$id = isset($_GET['id'])? $_GET['id'] : '';
		$id = isset($id)? $id : '';
		
		if(!empty($id)){ //1)如果有GET值
				
		  $sql = "SELECT * FROM `ci_article` WHERE `id`=?";
		  $qry = $this->db->query($sql,array($id));	
		
		  if($qry->num_rows() > 0){ //2)如果有返回文章資料
			 $row = $qry->row();
			
			 $this->load->view('frontend/default/header',$this->info);
		     $this->load->view('frontend/blog_edit',$row);
		     $this->load->view('frontend/default/footer');		 
		  }	
	   }else{
			 echo "<script>javascript:alert('抱歉!文章編號有誤!'); window.location = '".site_url('frontend/blog')."'</script>";
	   }	
		
	}
	
	//儲存編輯方法
	function edit_save(){
		
		//接收POST值(文章id)
		$id = $this->input->post('id');
				
		if($id === FALSE) {
		   echo "<script>javascript:alert('抱歉!文章更新失敗！'); window.location = '".site_url('frontend/blog')."'</script>";
		   exit;
		} 
		
		$data = array(		     
		   'category_id' => $this->input->post('category_id'),
		         'title' => $this->input->post('title'),
		       'content' => $this->input->post('content'),
		         'cdate' => $this->input->post('cdate'),
		);
		
		$this->db->where('id',$this->input->post('id'));
		$this->db->update('ci_article',$data);	
	
		echo "<script>javascript:alert('恭喜!文章更新成功！'); window.location = '".site_url('frontend/blog')."'</script>";
		
	}
	
	//關於我方法
	function about_me(){
		
		//接收POST值
		$login = $this->input->post('login');
		 $memo = $this->input->post('memo');
		
		$sql = "SELECT `memo` FROM `ci_member` WHERE `login`=?";
		$qry = $this->db->query($sql,array($login));
		
		if($qry->num_rows() > 0){ //如果存在該筆資料
			
			$data = array('memo' =>  $memo);	
				
		    //執行Update				
			$this->db->where('login',$login);
			$this->db->update('ci_member',$data);
			
			//導向blog頁面
			redirect('frontend/blog');	
		}		
	}
	
	//新增分類方法
	function category_add(){
		
		//表單規則驗證
		$this->form_validation->set_rules('category','分類','trim|required');	
		$this->form_validation->set_rules('cuser','使用者','trim|required');
		
		if($this->form_validation->run() === FALSE){
			echo "<script>javascript:alert('抱歉!分類欄位不得有空白！'); window.location = '".site_url('frontend/blog')."'</script>";
		    exit;
		    			
		}else{		
		//接收POST值
        $category = $this->input->post('category');
	       $cuser = $this->input->post('cuser');	
	       $cdate = $this->input->post('cdate');
	 	
	   	$data = array(
	        'category' => $category,
		       'cuser' => $cuser,
		       'cdate' => $cdate		
		);	
		
		$rlt = $this->db->insert('ci_category',$data);
		
		if($rlt === FALSE){
		   echo "<script>javascript:alert('抱歉!分類更新失敗！'); window.location = '".site_url('frontend/blog')."'</script>";
		   exit;
		}		
		
		echo "<script>javascript:alert('恭喜!分類更新成功！'); window.location = '".site_url('frontend/blog')."'</script>";		 	
	    }	
	    
	  }

	  
}


//*** 結束blog.php ***// 